<?php

namespace Admin\Controller;

//我的收藏

class CollectController extends AdminController {

    private $m;

    public function _initialize() {
        parent::_initialize();
        //产品分类名称
        $producttype = M("product_type")->field("id,name")->select();
        $this->assign("producttype", $producttype);
        //门店
        $store = M("store")->field("id,sname")->select();
        $this->assign("store", $store);
        $this->assign("meta_title", "收藏列表");
        $this->m = D("Collect");
    }

    public function index() {
        $where = array();
        $list = $this->lists($this->m->relation(TRUE), $where);
        //1门店 2产品  3设计师  4作品 
        foreach ($list as $key => $value) {
         if($value["type"]==1){
             $list[$key]["name"] =$value["sname"];
         }else if($value["type"]==2){
             $list[$key]["name"] =$value["pname"];
         }else if($value["type"]==3){
             $list[$key]["name"] =$value["dname"];
         }else if($value["type"]==4){
             $list[$key]["name"] =$value["wname"];
         }
         $where1["id"]=array("eq",$value["mid"]);
        $list[$key]["mname"] =  M("members")->where($where1)->getField("name");
        }
        $this->assign("list", $list);
        $this->display();
    }

    public function add() {
        if (IS_POST) {
            $this->m->create(I("post."));
            $result = $this->m->add();
            if ($result) {
                $this->success("操作成功！", U('index'));
            } else {
                $this->error($this->m->getError());
            }
        } else {
            $this->display();
        }
    }

    public function edit($id) {
        if (IS_POST) {
            $this->m->create(I("post."));
            $where["id"] = array("eq",I("id"));
            $result = $this->m->where($where)->save();
            if ($result) {
                $this->success("操作成功！", U('index'));
            } else {
                $this->error($this->m->getError());
            }
        } else {
            $info = $this->m->find($id);
            $this->assign("info", $info);
            $this->display("add");
        }
    }

    public function del() {
        $id = array_unique((array) I('id', 0));

        if (empty($id)) {
            $this->error('请选择要操作的数据!');
        }

        $map = array('id' => array('in', $id));
        if ($this->m->where($map)->delete()) {
            // S('DB_CONFIG_DATA',null);
            //记录行为
            action_log('delete_store', 'Store', $id, UID);
            $this->success('删除成功');
        } else {
            $this->error('删除失败！');
        }
    }

}
